package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.Util.ShiroUtil;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Blog;
import com.markerhub.entity.ElasticSearchBlog;
import com.markerhub.service.BlogService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    BlogService blogService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/blogs")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage){

        Page page=new Page(currentPage,5);
        IPage pageData =blogService.page(page,new QueryWrapper<Blog>().orderByDesc("created"));

        return Result.succ(pageData);
    }

    @GetMapping("/blog/{id}")
    public Result detail(@PathVariable(name = "id") Long id){

        Blog blog=blogService.getById(id);
        Assert.notNull(blog,"该博客已被删除");

        return Result.succ(blog);
    }

    //编辑or创建
    @RequiresAuthentication
    @PostMapping("/blog/edit")
    public Result edit(@Validated @RequestBody Blog blog){

        Blog temp=null;
        if(blog.getId()!=null){
            temp=blogService.getById(blog.getId());
            //只能编辑自己的文章
            Assert.isTrue(temp.getUserId() == ShiroUtil.getProfile().getId(),"没有权限编辑!");
        } else{

            temp =new Blog();
            temp.setUserId(ShiroUtil.getProfile().getId());
            temp.setCreated(LocalDateTime.now());
            temp.setStatus(0);
        }

        //参数位置>=3的参数：复制时忽略该属性
        BeanUtil.copyProperties(blog,temp,"id","userId","created","status");
        blogService.saveOrUpdate(temp);
        ElasticSearchBlog blogES=new ElasticSearchBlog();
        BeanUtil.copyProperties(temp,blogES);
        if(blog.getId()!=null){

            rabbitTemplate.convertAndSend("UpdateES",blogES);
        }else{
            rabbitTemplate.convertAndSend("InsertES",blogES);
        }
        return Result.succ(null);
    }

    @GetMapping("/Search/{pageNum}/{pageSize}")
    public Result Search(
            @PathVariable(name = "pageNum") int pageNum,
            @PathVariable(name = "pageSize") int pageSize,
            String keyword)throws Exception{
        System.out.println(keyword);
        System.out.println(pageNum);
        System.out.println(pageSize);
        return blogService.Search(keyword,pageNum,pageSize);
    }

    @RequiresAuthentication
    @GetMapping("/esUpdate")
    public Result esUpdate() throws IOException {
        return Result.succ(blogService.ElasticSearchUpdate());
    }


}
